package com.bc.mes.api.controller.dv;

import com.bc.mes.common.api.CommonPage;
import com.bc.mes.common.api.CommonResult;
import com.bc.mes.common.util.RedisUtil;
import com.bc.mes.device.service.DvMachineryService;
import com.bc.mes.mbg.model.dv.DvMachinery;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * --- 天道酬勤 ---
 *
 * @author zhangpingan
 * @date 2024/7/11
 * @desc
 */
@RequestMapping("/dv")
@Controller
@CrossOrigin(allowCredentials = "true")
@Api(tags = "DvMachinery",description = "设备")
public class DvMachineryController {

    @Autowired
    private DvMachineryService dvMachineryService;

    @Autowired
    private RedisUtil redisUtil;


    @ApiOperation(value = "查找设备")
    @ResponseBody
    @RequestMapping(value = "machinery/list",method = RequestMethod.GET)
    public CommonResult list(String machineryCode,String machineryName,Long machineryTypeId,
                            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){

        Page page = PageHelper.startPage(pageNum, pageSize);
        List<DvMachinery> list =  dvMachineryService.listMachinery(machineryCode,machineryName,machineryTypeId);
        Long total = page.getTotal();
        return CommonResult.success(CommonPage.restPage(list,total));
    }

    @ApiOperation(value = "根据id查找设备")
    @ResponseBody
    @RequestMapping(value ="machinery/{id}",method = RequestMethod.GET)
    public CommonResult getById(@PathVariable("id") Long id){
        return CommonResult.success(dvMachineryService.selectById(id));
    }

    @ApiOperation(value = "更新设备")
    @ResponseBody
    @RequestMapping(value = "machinery",method = RequestMethod.PUT)
    public CommonResult update (@RequestBody DvMachinery dvMachinery){
        int i  = dvMachineryService.update(dvMachinery);
        if(i>0){
            return CommonResult.success(200,"更新成功");
        }
        return CommonResult.failed();
    }

}
